﻿using Sunny.UI;
using System;
using System.Security.Cryptography;
using System.Text;

namespace Toolbox.form.box
{
    public partial class EncryptForm : UIPage
    {
        public EncryptForm()
        {
            InitializeComponent();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            // SHA256加密按钮
            string str = uiTextBox1.Text;
            string encryptStr = EncryptSha256(str).ToUpper();
            uiTextBox2.Text = encryptStr;
        }

        private String EncryptSha256(String str)
        {
            string result = null;

            byte[] bytes = Encoding.UTF8.GetBytes(str);
            byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }
            result = sb.ToString();
            return result;
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            // MD5加密
            string str = uiTextBox3.Text;
            String oneMd5 = EncryptMd5(str);
            uiTextBox4.Text = oneMd5.ToUpper();
            uiTextBox5.Text = oneMd5;

            String twoMd5 = EncryptMd5(oneMd5);
            uiTextBox6.Text = twoMd5.ToUpper();
            uiTextBox7.Text = twoMd5;
        }

        private String EncryptMd5(String str)
        {
            string result = null;

            byte[] bytes = Encoding.UTF8.GetBytes(str);
            byte[] hash = MD5.Create().ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }
            result = sb.ToString();
            return result;
        }

        
    }
}
